import {
    GetGroupListWithPage,
    CreateFirstGroup,
    UpdateFirstGroup,
    DelFirstGroup,
    CreateSecondGroup,
    UpdateSecondGroup,
    DelSecondGroup,
    GetFirstGroupById,
    ChangeSecondGroupSort,
    ChangeFirstGroupSort,
} from '@/services/product/productClassify';
import {catchException} from '@/utils/utils';

export default {
    namespace: 'productClassify',

     state: {
        productClassify: [],
        productFirstGroup: {}
     },

     effects: {

        // 获取
         *GetGroupListWithPage({payload, callBack}, {call, put}) {
            const data = yield call(GetGroupListWithPage, payload);
            yield catchException(function* (){
              if(data.Code ===0) {
                yield put({
                    type: 'saveGetGroupListWithPage',
                    payload: {
                        DataList: data.Data,
                    },
                });
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },

        // 获取一级
        *GetFirstGroupById({payload, callBack}, {call, put}) {
            const data = yield call(GetFirstGroupById, payload);
            yield catchException(function* (){
              if(data.Code ===0) {
                yield put({
                    type: 'saveGetFirstGroupById',
                    payload: {
                        data: data.Data,
                    },
                });
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },



        // 新增一级
        *CreateFirstGroup({ payload, callBack }, { call, put }) {
            const data = yield call(CreateFirstGroup, payload);
            yield catchException(function* (){
              if (data.Code === 0) {
                callBack  && callBack.success(data);
              }else{
                  callBack && callBack.error(data);
              }
            })

        },

        // 修改一级
        *UpdateFirstGroup({payload, callBack}, {call}) {
            const data = yield call(UpdateFirstGroup, payload);
            yield catchException(function* (){
              if(data.Code === 0) {
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },

        // 修改二级排序
        *ChangeSecondGroupSort({payload, callBack}, {call}) {
        const data = yield call(ChangeSecondGroupSort, payload);
        yield catchException(function* (){
          if(data.Code === 0) {
            callBack && callBack.success(data);
        } else {
            callBack && callBack.error(data)
            }
        })

      },

        // 修改一级排序
        *ChangeFirstGroupSort({payload, callBack}, {call}) {
        const data = yield call(ChangeFirstGroupSort, payload);
        yield catchException(function* (){
          if(data.Code === 0) {
            callBack && callBack.success(data);
        } else {
            callBack && callBack.error(data)
            }
        })

        },

        // 删除一级
        *DelFirstGroup({payload, callBack},{call}) {
            const data = yield call(DelFirstGroup, payload);
            yield catchException(function* (){
              if (data.Code === 0) {
                callBack && callBack.success(data);
                } else {
                callBack && callBack.error(data)
                }
            })

        },

        // 新增二级
        *CreateSecondGroup({ payload, callBack }, { call, put }) {
            const data = yield call(CreateSecondGroup, payload);
            yield catchException(function* (){
              if (data.Code === 0) {
                callBack && callBack.success(data);
            }else{
                callBack && callBack.error(data);
            }
            })

        },

        //修改二级
        *UpdateSecondGroup({ payload, callBack }, {call}) {
            const data = yield call(UpdateSecondGroup, payload);
            yield catchException(function* (){
              if(data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
            })

        },

        //删除二级
        *DelSecondGroup({payload, callBack},{call}) {
            const data = yield call(DelSecondGroup, payload);
            yield catchException(function* (){
              if (data.Code === 0) {
                callBack && callBack.success(data);
                } else {
                callBack && callBack.error(data)
                }
            })

        }
     },

     reducers: {
        saveGetGroupListWithPage(state, {payload}) {
            return {
                ...state,
                productClassify: payload.DataList,
            };
        },

        saveGetFirstGroupById(state, {payload}) {
            return {
                ...state,
                productFirstGroup: payload,
            };
        }
    }
}
